Imports DotNetNuke
Imports System.Web.UI
Imports DotNetNuke.Services.FileSystem

Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Modules.Actions


Namespace OpenLight.Modules.ConfigEditor

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The View class displays the content
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Partial Class Restore
        Inherits Entities.Modules.PortalModuleBase
        Implements IActionable



        Private configPath As String = HttpContext.Current _
                                        .Server.MapPath("~/web.config")

#Region "Event Handlers"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then

                    If Me.UserInfo.IsSuperUser Then

                        Me.EditorPanel.Visible = True

                    Else

                        Me.EditorPanel.Visible = False
                        Me.StatusLabel.Text = "You must be a SuperUser to use this module."

                    End If


                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region


        Protected Sub RestoreButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RestoreButton.Click

            If Me.UserInfo.IsSuperUser Then

                Dim fileId As String = RestoreFileUrlControl.Url.Replace("FileID=", String.Empty)

                If RestoreFileUrlControl.UrlType = "F" _
                    AndAlso IsNumeric(fileId) Then

                    Try

                        Dim fileCtrl As New FileController

                        Dim file As FileInfo = fileCtrl.GetFileById(Integer.Parse(fileId), Me.PortalId)

                        IO.File.WriteAllText(configPath, IO.File.ReadAllText(file.PhysicalPath))

                        Me.StatusLabel.Text = "Web.config has been restored."

                    Catch ex As Exception

                        ProcessModuleLoadException("Web.Config could not be restored.", Me, ex, True)


                    End Try
                End If

            Else

                Me.StatusLabel.Text = "You must be a SuperUser to use this module."


            End If

        End Sub

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim actions As New Actions.ModuleActionCollection

                actions.Add(GetNextActionID(), "Return", _
                                "", "", "", NavigateURL(), False, _
                                SecurityAccessLevel.Host, True, False)

                Return actions
            End Get
        End Property

        Protected Sub ReturnLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReturnLinkButton.Click

            Response.Redirect(NavigateURL())

        End Sub
    End Class

End Namespace
